// MainForm.cpp : implementation file
//

#include "stdafx.h"
#include "ConsoleForm.h"
#include "Redirect.h"
const int FILENAMES_BUF_SIZE	= 1024;

// CConsoleForm dialog

IMPLEMENT_DYNAMIC(CConsoleForm, CBaseForm)

CConsoleForm::CConsoleForm(CWnd* pParent /*=NULL*/)
	: CBaseForm(CConsoleForm::IDD, pParent)
{
m_pRedirect = NULL;
}

CConsoleForm::~CConsoleForm()
{
}

void CConsoleForm::DoDataExchange(CDataExchange* pDX)
{
	CBaseForm::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRedirectDemoDlg)
	//DDX_Control(pDX, IDOK, m_BtnOk);
	DDX_Control(pDX, IDC_EDIT_RUN, m_EditRun);
	DDX_Control(pDX, IDC_EDIT_OUTPUT, m_EditOutput);
	DDX_Control(pDX, IDC_BUTTON_STOP, m_BtnStop);
	DDX_Control(pDX, IDC_BUTTON_RUN, m_BtnRun);
	DDX_Control(pDX, IDC_BUTTON_BROWSE, m_BtnBrowse);
	DDX_Check(pDX, IDC_CHECK_CLEAR, m_bClearOutput);
	//}}AFX_DATA_MAP

}


BEGIN_MESSAGE_MAP(CConsoleForm, CBaseForm )

	ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CConsoleForm::OnBnClickedButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_RUN, &CConsoleForm::OnBnClickedButtonRun)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CConsoleForm::OnBnClickedButtonStop)
	ON_EN_UPDATE(IDC_EDIT_RUN, &CConsoleForm::OnEnUpdateEditRun)
	//ON_EN_UPDATE(IDC_EDIT_RUN, OnUpdateEditRun)

END_MESSAGE_MAP()

CString CConsoleForm::GetTitle() const
{
	return _T("Console Form");
}

BOOL CConsoleForm::AllowClose()
{
	//throw std::exception("The method or operation is not implemented.");
	return TRUE;
}



BOOL CConsoleForm::OnInitDialog()
{
	CBaseForm::OnInitDialog();



	//--------------------------------------------------------------------------
	//	Create a fix-spaced font to use in output edit control.
	//--------------------------------------------------------------------------
	CClientDC dc(this);
	int	nHeight = -((dc.GetDeviceCaps(LOGPIXELSY) * 10) / 72);

	m_Font.CreateFont
		(
		nHeight,
		0,
		0,
		0,
		FW_NORMAL,
		0,
		0,
		0,
		DEFAULT_CHARSET,
		OUT_CHARACTER_PRECIS,
		CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_DONTCARE,
		_T("Courier New")
		);

	m_EditOutput.SetFont(&m_Font);
	m_BtnStop.EnableWindow(FALSE);
	m_BtnRun.EnableWindow(FALSE);	

	m_EditRun.LimitText(0x20000);


	return TRUE;  // return TRUE  unless you set the focus to a control
}

// CConsoleForm message handlers

void CConsoleForm::OnBnClickedButtonBrowse()
{
	// TODO: Add your control notification handler code here
	static TCHAR	szFilter[] = _T("Executable Files (*.exe;*.bat)|*.exe;*.bat|||");
	TCHAR			filenamesBuf[FILENAMES_BUF_SIZE];

	CFileDialog	dlg
		(
		TRUE,	// TRUE for FileOpen, FALSE for FileSaveAs
		NULL,
		NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		szFilter,
		NULL
		);

	filenamesBuf[0]      = 0;
	dlg.m_ofn.lpstrFile  = filenamesBuf;
	dlg.m_ofn.nMaxFile   = FILENAMES_BUF_SIZE;

	int nResponse = dlg.DoModal();

	if (nResponse == IDOK)
	{
		CString Command = dlg.GetPathName();
		m_EditRun.SetWindowText(Command);
	}

}

void CConsoleForm::OnBnClickedButtonRun()
{
	// TODO: Add your control notification handler code here
	CString Command;

	UpdateData();

	if ( m_bClearOutput )
	{
		m_EditOutput.SetWindowText(_T(""));
	}

	m_EditRun.GetWindowText(Command);

	m_pRedirect = new CRedirect((LPTSTR)(LPCTSTR)Command,  &m_EditOutput);

	UpdateControls();

	m_pRedirect->Run();

	delete m_pRedirect;

	m_pRedirect = 0;

	UpdateControls();

	m_EditRun.SetFocus();
	m_EditRun.SetSel(0, -1);

}

void CConsoleForm::OnBnClickedButtonStop()
{
	// TODO: Add your control notification handler code here
	if ( m_pRedirect )
	{
		m_pRedirect->Stop();
	}	

}

void CConsoleForm::UpdateControls()
{
	

	
	if ( m_pRedirect )
	{
		m_EditRun.EnableWindow(FALSE);
		m_BtnBrowse.EnableWindow(FALSE);
		m_BtnRun.EnableWindow(FALSE);
		m_BtnStop.EnableWindow(TRUE);
		//m_BtnOk.EnableWindow(FALSE);

		//----------------------------------------------------------------------
		//	Disable Close in System Menu (this also disables close box).
		//----------------------------------------------------------------------
		
	}
	else 
	{
		m_EditRun.EnableWindow(TRUE);
		m_BtnBrowse.EnableWindow(TRUE);
		m_BtnRun.EnableWindow(m_EditRun.GetWindowTextLength() > 0);
		m_BtnStop.EnableWindow(FALSE);
		//m_BtnOk.EnableWindow(TRUE);

		//----------------------------------------------------------------------
		//	Enable Close in System Menu (this also enables close box).
		//----------------------------------------------------------------------
		
	}
}

void CConsoleForm::OnEnUpdateEditRun()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CBaseForm::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	// TODO:  Add your control notification handler code here
		UpdateControls();	
}

